package com.itruth.module.main;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.itruth.base.BaseActivity;
import com.itruth.data.entity.UserData;
import com.itruth.main.R;
import com.itruth.module.look.LookActivity;
import com.itruth.module.record.RecordActivity;
import com.itruth.module.reg.RegActivity;
import com.itruth.module.share.ShareActivity;
import com.itruth.util.Constant;

public class MainActivity extends BaseActivity {

	@Override
	protected void initData() {
		
	}
	
	@Override
	protected void initComposition() {
		setContentView(R.layout.main);
		
		ImageView loginView = (ImageView)findViewById(R.id.iv_login);
		
		loginView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this, RegActivity.class));
			}
		});
		
		GridView leftGridView = (GridView)findViewById(R.id.gridview_left);
		GridView rightGridView = (GridView)findViewById(R.id.gridview_right);
		
		leftGridView.setAdapter(new MainGridViewAdapter(this, true));
		rightGridView.setAdapter(new MainGridViewAdapter(this, false));
		
		leftGridView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				if (position == 0) {
					// TODO 限制每天最大保存次数
					if (Constant.MAX_WRITE_TIMES <= UserData.getIns()
							.getWriteTimes()) {
						Toast.makeText(MainActivity.this, "已达到每天最大书写次数",
								Toast.LENGTH_SHORT).show();
						return;
					}
					Intent intent = new Intent(MainActivity.this,
							RecordActivity.class);
					startActivity(intent);

				} else if (position == 1) {
					Intent intent = new Intent(MainActivity.this,
							ShareActivity.class);
					startActivity(intent);
				}
				
			}
		});
		
		rightGridView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 0) {
					Intent intent = new Intent(MainActivity.this, LookActivity.class);
					startActivity(intent);
				} else if (position == 1) {
					Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
				}
			}
			
		});
		
		
	}
	
}
